package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.entity.WorkOrder;
import com.woniuxy.factory.entity.WorkShop;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.service.WorkOrderService;
import com.woniuxy.factory.service.WorkShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/workorder")
public class WorkOrderController {

    @Autowired
    WorkOrderService wos;
    @Autowired
    WorkShopService ws;

    //查询所有的派单信息
    @RequestMapping("allworkorder")
    @ResponseBody
    public ResponseResult selectAllWorkOrder(WorkOrder workOrder,int page,int limit){
        System.out.println(workOrder);
        List<WorkOrder> lwo = wos.selectWorkOrders(workOrder,page,limit);
        //查询派单总数
        int count = wos.selectWorkOrderCount(workOrder);
        ResponseResult rr = new ResponseResult(0,"",count,lwo);
        return rr;
    }

    //新增派单前先查询是否已经派单
    @ResponseBody
    @RequestMapping("/selectWorkOrderById")
    public ResponseResult selectWorkOrderById(String planid){
       ResponseResult rr;
        try{
            wos.selectWorkOrderById(planid);
            rr = new ResponseResult(0,"",0,null);
        }catch (BusinessException b){
            rr = new ResponseResult(1,b.getMessage(),0,null);
        }
        return rr;
    }

    //新增派单
    @ResponseBody
    @RequestMapping("/addworkorder")
    public ResponseResult addWorkOrder(WorkOrder workOrder){
       ResponseResult rr;
        try {
            wos.insertWorkOrder(workOrder);
            rr = new ResponseResult(0,"派单成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(2,"派单失败",0,null);
        }
        return rr;
    }

    //修改之前先判断当前计划单是否已经投入生产，已生成就不能修改
    @ResponseBody
    @RequestMapping("/schedulebyplanid")
    public ResponseResult selectScheduleByplanId(String planid){
        ResponseResult rr;
        try {
            wos.selectScheduleByplanId(planid);
            rr = new ResponseResult(0,"",0,null);
        }catch(BusinessException b){
            rr = new ResponseResult(1,b.getMessage(),0,null);
        }
        return rr;
    }

    //修改信息回显
    @ResponseBody
    @RequestMapping("/workorderecho")
    public Map<String, Object> workOrderEchoById(String worId){
        //定义map集合存储派单信息和所有车间
        Map<String,Object> map = new HashMap<String,Object>();
        //查询当前id的派单信息
        WorkOrder workOrder = wos.workOrderEchoById(worId);
        //查询所有的车间
        List<WorkShop> lws = ws.findWorkShop();
        //存入map集合
        map.put("workOrder",workOrder);
        map.put("workShop",lws);
        return map;
    }

    //修改
    @ResponseBody
    @RequestMapping("/updateworkorder")
    public ResponseResult updateWorkOrderById(WorkOrder workOrder){
        ResponseResult rr;
        try {
            wos.updateWorkOrderById(workOrder);
            rr = new ResponseResult(0,"修改成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(1,"修改失败",0,null);
        }
        return rr;
    }

    //删除
    @ResponseBody
    @RequestMapping("/deleteworkorder")
    public ResponseResult deleteWorkOrderById(String worId){

        ResponseResult rr;
        try {
            wos.deleteWorkOrderById(worId);
            rr = new ResponseResult(0,"删除成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(1,"删除失败",0,null);
        }
        return rr;
    }

    //删除选中
    @ResponseBody
    @RequestMapping("/deleteselected")
    public ResponseResult deleteWorkOrderSelected(@RequestParam("isStr[]") int[] worIds){
        ResponseResult rr;
        try {
            wos.deleteWorkOrderSelected(worIds);
            rr = new ResponseResult(0,"删除成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(1,"删除失败",0,null);
        }
        return rr;
    }
}
